package com.ruoyi.project.erp.controller;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.erp.dto.SubmitWorkLogDTO;
import com.ruoyi.project.erp.dto.WorklogQuery;
import com.ruoyi.project.erp.vo.WorklogCalendarVO;
import com.ruoyi.project.erp.vo.WorklogVO;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.erp.domain.Worklog;
import com.ruoyi.project.erp.service.IWorklogService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 工时Controller
 * 
 * @author ruoyi
 * @date 2025-06-19
 */
@RestController
@RequestMapping("/erp/worklog")
public class WorklogController extends BaseController
{
    @Autowired
    private IWorklogService worklogService;

    /**
     * 查询工时列表
     */
    @PreAuthorize("@ss.hasPermi('erp:worklog:list')")
    @GetMapping("/list")
    public TableDataInfo list(WorklogQuery query) // 1. 修改参数类型为 WorklogQuery
    {
        startPage();
        // 2. 调用 service 的新方法，返回值类型是 List<WorklogVO>
        List<WorklogVO> list = worklogService.selectList(query);
        return getDataTable(list);
    }

    /**
     * 导出工时列表
     */
    @PreAuthorize("@ss.hasPermi('erp:worklog:export')")
    @Log(title = "工时", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, WorklogQuery query)
    {
        List<WorklogVO> list = worklogService.selectExportList(query);
        ExcelUtil<WorklogVO> util = new ExcelUtil<WorklogVO>(WorklogVO.class);
        util.exportExcel(response, list, "工时数据");
    }

    /**
     * 获取工时详细信息
     */
    @PreAuthorize("@ss.hasPermi('erp:worklog:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(worklogService.selectWorklogById(id));
    }

    /**
     * 新增工时
     */
    @PreAuthorize("@ss.hasPermi('erp:worklog:add')")
    @Log(title = "工时", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Worklog worklog)
    {
        return toAjax(worklogService.insertWorklog(worklog));
    }

    /**
     * 新增工时
     */
    @PreAuthorize("@ss.hasPermi('erp:worklog:add')")
    @Log(title = "工时", businessType = BusinessType.INSERT)
    @PostMapping("/submitWorklog")
    public AjaxResult submitWorklog(@RequestBody Worklog submitWorkLogDTO)
    {
        return toAjax(worklogService.submitWorklog(submitWorkLogDTO));
    }


    /**
     * 修改工时
     */
    @PreAuthorize("@ss.hasPermi('erp:worklog:edit')")
    @Log(title = "工时", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Worklog worklog)
    {
        return toAjax(worklogService.updateWorklog(worklog));
    }

    /**
     * 删除工时
     */
    @PreAuthorize("@ss.hasPermi('erp:worklog:remove')")
    @Log(title = "工时", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(worklogService.deleteWorklogByIds(ids));
    }







    /**
     * 获取工时日历数据
     *
     * @param month 查询月份, 格式: "yyyy-MM"
     * @return 统一响应结果
     */
    @GetMapping("/calendar")
    public AjaxResult getCalendarData(@RequestParam("month") String month) {
        List<WorklogCalendarVO> list = worklogService.getWorklogCalendarData(month);
        return AjaxResult.success(list);
    }



}
